// xc_lshop/shop/user/barter/money/activation.js
// var barter_order = require('../../../../../model/barter_order')
var barter_order = require('../../../model/barter_order')

Page({
  mixins: [require('../../../../../xc/mixins/basicMixin')],

  /**
   * 页面的初始数据
   */
  data: {
    doing: false,
  },
  submit() {
    var that = this
    if (that.data.doing) {
      return
    }
    if (this.data.amount <= 0) {
      wx.showToast({
        icon: "none",
        title: '金额不能为0',
      })
    } else {
      var data = {}
      var checked=this.data.checked;

      data['checked'] = [];
      for(var i in checked){
        if(checked[i]){
          data['checked'].push(i)
        }
      }

      // console.log(data);
      // return
      var that = this;
      that.setData({
        doing: true
      })
      barter_order.createorder(data).then(function (restmodel) {

        if (restmodel["status"] == 1) {
          // var orderbill=restmodel["obj"];
          // var template_ids = restmodel["template_ids"]

          if (restmodel["paytype"] == 1) {
            var pay_params = restmodel["pay_params"];
            wx.requestPayment({
              timeStamp: pay_params["timeStamp"],
              nonceStr: pay_params["nonceStr"],
              package: pay_params["package"],
              signType: pay_params['signType'],
              paySign: pay_params["paySign"],
              success(res) {
                // that.showtemplate(template_ids, restmodel['ordernum'], restmodel["id"])
                that.query(restmodel)


              },
              fail(res) {
                if (res["errMsg"] == "requestPayment:fail cancel") {
                  wx.showModal({
                    title: '订单未支付',
                    content: '订单未支付',
                    showCancel: false,
                    success(res) {


                      // wx.redirectTo({
                      //   url: "../order/index?opstatus=-1",
                      // })
                    }
                  })
                }
                that.setData({
                  doing: false
                })
              }
            })


          } else if (restmodel["paytype"] == 0) {
            that.query({
              id: restmodel["id"]
            })
          }
        }
      })
    }
  },
  query(restmodel) {
    var that = this;
    barter_order.queryorder(restmodel["id"]).then(function (queryreslut) {
      if (queryreslut["status"] == 1) {

        // that.showtemplate(template_ids, restmodel['ordernum'], restmodel["id"])

        wx.showModal({
          title: '成功',
          content: "已兑换成功",
          cancelText: "返回",
          confirmText: "查看记录",
          complete: (res) => {
            if (res.cancel) {
              that.pageback('', true)
            }

            if (res.confirm) {
              wx.redirectTo({
                url: '/xc_lshop/shop/user/barter/record/list',
              })
            }
          }
        })
      }
      that.setData({
        doing: false
      })
    })
  },
  xcloaddata: function (options, config) {
    var update = {};
    if (config.payment) {
      update["paymentconfig"] = config.payment
    }
    if (config.yihuo) {
      if (config.yihuo.status == 1) {
        update["yihuoconfig"] = config.yihuo
      } else {
        this.error("易货功能未开启", true)
      }
    }
    console.log("update")
    console.log(update)
    this.setData(update)

    this.selectComponent("#order").initmoney()
  },
  onchoose(e) {
    var con = e.detail;
    var payprice = con.amount * this.data.yihuoconfig.activeval * 0.01
    this.setData({
      amount: con.amount,
      checked: con.checked,
      payprice: payprice.toFixed(0)
    })
  },
  chooseorder() {
    this.selectComponent("#order").init()
  },
  afterlogin: function (options, config, userinfo) {
    var that = this;

    that.setData({
      "member": userinfo
    })
    if (userinfo.member_id == 0) { //未登录
      this.login()
    }
  },
  // 登录注册
  login() {
    var that = this;
    app.xc_sys_config.authop.oppower("login").then(function (res) {
      that.setData({
        "member": app.globalData.member
      })
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})